package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.SensitiveDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.admin.AdSensitive;
import com.heima.wemedia.mapper.AdminSensitiveMapper;
import com.heima.wemedia.service.AdminSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminSensitiveServiceImpl extends ServiceImpl<AdminSensitiveMapper, AdSensitive> implements AdminSensitiveService {

    @Autowired
    private AdminSensitiveMapper adminSensitiveMapper;


    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delById(Integer id) {
        if (id == null)throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);

        adminSensitiveMapper.deleteById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null) throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);

        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        wrapper.like(AdSensitive::getSensitives,dto.getName());
        //按创建时间倒序排列
        wrapper.orderByDesc(AdSensitive::getCreatedTime);
        //构建分页page
        Page<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        //进行分页条件查询
        page(page,wrapper);

        //封装结果返回
        com.heima.model.common.dtos.PageResponseResult pr = new PageResponseResult((int)page.getCurrent(),(int)page.getSize(),(int)page.getTotal());
        pr.setData(page.getRecords());
        return pr;
    }

    /**
     * 保存敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveById(AdSensitive adSensitive) {
        if (adSensitive == null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        AdSensitive sensitive = adminSensitiveMapper.selectOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));

        if (sensitive != null)
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);

        adminSensitiveMapper.insert(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive == null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);

        adminSensitiveMapper.updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
